package org.mte.sak.xml;

import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.util.HashMap;

import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

/**
 * XMLSchemaValidator: validate a XML file against a XML Schema
 *
 * @author Marco Traversari
 * @version $Revision: 1.1 $ $Date: 2011-04-18 08:38:19 $ $Author: marco $
 */
public class XMLSchemaValidator {

	private SchemaFactory schemaFactory = null;
	private DocumentBuilder parser = null;
	private HashMap<String, Validator> validators = null;

	/**
	 * XMLSchemaValidator
	 * @throws ParserConfigurationException
	 */
	public XMLSchemaValidator() throws ParserConfigurationException {
		this.schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
		//schemaFactory.setErrorHandler(new XMLErrorHandler());
		this.parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
	}

	/**
	 * validate
	 * @param xmlFile
	 * @param xmlSchemaFile
	 * @throws Exception
	 */
	public void validate(File xmlFile, File xmlSchemaFile) throws Exception {
		try {
			Validator validator = retrieveValidator(xmlSchemaFile);
			Document document = parser.parse(xmlFile);
			validator.validate(new DOMSource(document));
		} catch (SAXException se) {
			//schemaFactory.getErrorHandler().error((SAXParseException) se);
			throw se;
		} catch (IOException ioe) {
			ioe.printStackTrace();
			throw ioe;
		}
	}

	/**
	 * validate
	 * @param xml
	 * @param schemaName
	 * @param schema
	 * @throws Exception
	 */
	public void validate(String xml, String schemaName, String schema) throws Exception {
		try {
			Validator validator = retrieveValidator(schemaName, schema);
			Document document = parser.parse(new InputSource(new StringReader(xml)));
			validator.validate(new DOMSource(document));
		} catch (SAXException se) {
			//schemaFactory.getErrorHandler().error((SAXParseException) se);
			throw se;
		} catch (IOException ioe) {
			ioe.printStackTrace();
			throw ioe;
		}
	}

	/**
	 * retrieveValidator
	 * @param schemaName
	 * @param schema
	 * @return validator
	 * @throws SAXException
	 */
	private Validator retrieveValidator(String schemaName, String schema) throws SAXException {
		initValidators();
		Validator validator = validators.get(schemaName);
		if (validator == null) {
			Schema schemaXSD = schemaFactory.newSchema(new StreamSource(new StringReader(schema)));
			validator = schemaXSD.newValidator();
			validators.put(schemaName, validator);
		}
		return validator;
	}


	/**
	 * retrieveValidator
	 * @param fileSchemaPath
	 * @return validator
	 * @throws SAXException
	 */
	private Validator retrieveValidator(File xmlSchemaFile) throws SAXException {
		initValidators();
		Validator validator = validators.get(xmlSchemaFile.getName());
		if (validator == null) {
			Schema schemaXSD = schemaFactory.newSchema(xmlSchemaFile);
			validator = schemaXSD.newValidator();
			validators.put(xmlSchemaFile.getName(), validator);
		}
		return validator;
	}

	/**
	 * initValidators
	 */
	private void initValidators() {
		if (validators == null) {
			validators = new HashMap<String, Validator>();
		}
	}
}
